<?php

namespace app\admin\controller;

use app\admin\middleware\Auth;
use app\model\PostWhiteUserModel;
use app\model\UserFbLogModel;
use app\model\UserModel;
use think\App;

class UserFbLog extends Base {

    protected $middleware = [Auth::class];
    private $model;

    private $aid;

    private $page;

    private $limit;


    public function __construct(App $app){

        parent::__construct($app);

        $this->req = request()->param();

        $this->model = new UserFbLogModel();

        $this->aid = $this->isLogin();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 20;
    }

    /*
   * 获取用户信息
   * */
    public function getList(){

        if(!$this->req["uid"]){
            return $this->fail("请选择用户");
        }

        $where=[];
        $where[] = ["uid","=",$this->req["uid"]];
        $where[] = ["fb_status","=",1];

        $list = $this->model->getList($where,$this->page,$this->limit,"created_at");

        $total =  $this->model->getTotal($where);

        if(empty($list)){
            return $this->succ();
        }
        return $this->succData($total,$list);

    }



    /*
     * 新增用户的福贝支出
     *
     * */

    public function donateFb()
    {

        if (!$this->req["uid"]) {
            return $this->fail("请选择要操作的用户");
        }

        if (!$this->req["title"]) {
            return $this->fail("名目不能为空");
        }

        if (!$this->req["description"]) {
            return $this->fail("名称不能为空");
        }

        if (!$this->req["fb"]) {
            return $this->fail("支出的福贝不能为空");
        }

        if ($this->req["fb"] < 0) {
            return $this->fail("支出的福贝不能为负数");
        }

        $fbAmount = (int)($this->req["fb"] * 100);
        if ( $fbAmount > $this->model->getUserTotalFb($this->req["uid"],false)) {
            return $this->fail("用户剩余福贝数不足");
        }


        $fbLog[] = [
            "uid" => $this->req["uid"],
            "fb" => "-".$fbAmount,
            "type" => 'donate',
            "title" => $this->req["title"],
            "description" => $this->req["description"],
            "is_admin" => 1,
            "fb_status"=>1
        ];


        $res = $this->model->batchAdd($fbLog);

        if ($res) {
            return $this->succ("操作成功");
        } else {
            return $this->fail("操作失败");
        }
    }


    /*
     *修改捐助记录
     *
     * */

    public function editDonateFbLog()
    {

        if (!$this->req["id"]) {
            return $this->fail("请选择要操作的用户");
        }

        if (!$this->req["title"]) {
            return $this->fail("名目不能为空");
        }

        if (!$this->req["description"]) {
            return $this->fail("名称不能为空");
        }

        if (!$this->req["fb"]) {
            return $this->fail("支出的福贝不能为空");
        }

        if ($this->req["fb"] < 0) {
            return $this->fail("支出的福贝不能为负数");
        }

        $old = $this->model->where("id","=",$this->req["id"])->find();
        if($old["is_admin"] != 1){
            return $this->fail("非后台操作记录，不允许修改");
        }

        $fbAmount = (int)($this->req["fb"] * 100);
        if ($fbAmount > $this->model->getUserTotalFb($old["uid"],false) + abs($old["fb"])) {
            return $this->fail("用户剩余福贝数不足");
        }

        $fbLog = [
            "fb" => "-".$fbAmount,
            "title" => $this->req["title"],
            "description" => $this->req["description"],
        ];

        $where=[];
        $where[] = ["id","=",$this->req["id"]];

        $res = $this->model->updateAll($where,$fbLog);

        if ($res) {
            return $this->succ("操作成功");
        } else {
            return $this->fail("操作失败");
        }
    }


    /*
     *删除捐助记录
     *
     * */

    public function delDonateFbLog()
    {

        if (!$this->req["id"]) {
            return $this->fail("请选择要操作的用户");
        }


        $old = $this->model->where("id","=",$this->req["id"])->find();
        if($old["is_admin"] != 1){
            return $this->fail("非后台操作记录，不允许修改");
        }

        $where=[];
        $where[] = ["id","=",$this->req["id"]];

        $res = $this->model->where($where)->delete();

        if ($res) {
            return $this->succ("操作成功");
        } else {
            return $this->fail("操作失败");
        }
    }
}